/*
 * @Author : restyhap
 * @Date   : 2023-01-23 20:00:22
 * @LastEditors: restyhap
 * @LastEditTime: 2023-01-24 00:17:42
 */

const vscode = require('vscode');
const { getTypeData } = require('./typeData.js');

function activate (context) {
  let textEditor = vscode.window.activeTextEditor;
  // 当编辑器文本变化时，重置编辑器对象
  vscode.window.onDidChangeActiveTextEditor((editor) => (textEditor = editor));

  let disposable = vscode.commands.registerCommand('resty-multiline-annotation.annotation', function () {

    textEditor.edit((editorBuilder) => {
      let data = getTypeData(textEditor);
      let document = textEditor.document;
      let selection = textEditor.selection;
      // 获取光标内容     1. 多行选中 未注释  2. 多行选中 已注释   3. 单行
      let currentLine = selection.active.line;  // 光标所在行
      let anchorLine = selection.anchor.line;  // 锚点行

      if (data.fileSuffix != undefined) {
        if (currentLine != anchorLine) {
          if (currentLine > anchorLine) {
            currentLine = currentLine + anchorLine;
            anchorLine = currentLine - anchorLine;
            currentLine = currentLine - anchorLine;
          }
          // 1. 多行选中 未注释
          console.log(" currentLine ======== " + currentLine);
          console.log(" anchorLine ======== " + anchorLine);
          let startLine = currentLine;
          let endLine = anchorLine;
          let startBool = false;
          let endBool = false;
          // 如果从开始往下找没找到前缀返回 false , 找到返回 true,并记录当前行。
          while (startLine < anchorLine) {
            if (startBool = document.lineAt(startLine).text.includes(data.prefix)) {
              break;
            }
            startLine++;
          }
          // 从结束往上找 没找到后缀返回 false , 找到返回 false,并记录当前行。
          while (endLine > currentLine) {
            if (endBool = document.lineAt(endLine).text.includes(data.suffix)) {
              break;
            }
            endLine--;
          }
          // 没有前缀也没有后缀的情况下 添加多行注释
          if (!startBool && !endBool) {
            // insert(currentLine, anchorLine, document, editorBuilder);
            let startLength = document.lineAt(currentLine).text.length;
            let text = document.lineAt(currentLine).text;
            let space = "";
            if (document.lineAt(currentLine).firstNonWhitespaceCharacterIndex != 0) {
              let spaceCount = document.lineAt(currentLine).firstNonWhitespaceCharacterIndex;
              for (let i = 0; i < spaceCount; i++) {
                space += " ";
              }
              text = text.replace(/^\s*/, "");
            }
            let startText = space + data.prefix + text;
            let endLength = document.lineAt(anchorLine).text.length;
            let endText = document.lineAt(anchorLine).text + data.suffix;
            editorBuilder.replace(new vscode.Range(new vscode.Position(currentLine, 0), new vscode.Position(currentLine, startLength)), "");
            editorBuilder.insert(new vscode.Position(currentLine, 0), startText);
            editorBuilder.replace(new vscode.Range(new vscode.Position(anchorLine, 0), new vscode.Position(anchorLine, endLength)), "");
            editorBuilder.insert(new vscode.Position(anchorLine, 0), endText);
          } else {
            if (startBool && !endBool) {
              while (endLine < document.lineCount) {
                endLine++;
                if (endBool = document.lineAt(endLine).text.includes(data.suffix)) {
                  break;
                }
              }
            }
            if (!startBool && endBool) {
              while (startLine > 0) {
                startLine--;
                if (startBool = document.lineAt(startLine).text.includes(data.prefix)) {
                  break;
                }
              }
            }
            if (startBool && endBool) {
              clear(startLine, endLine, document, editorBuilder);
            }
          }
        } else {
          // 3. 单行时清除
          // clear(currentLine, document, data, editorBuilder);
          let startLine = currentLine;
          let endLine = currentLine;
          while (startLine > 0) {
            startLine--;
            let text = document.lineAt(startLine).text;
            if (text.includes(data.prefix)) {
              break;
            }
          }
          while (endLine < document.lineCount) {
            endLine++;
            let text = document.lineAt(endLine).text;
            if (text.includes(data.suffix)) {
              break;
            }
          }
          clear(startLine, endLine, document, editorBuilder);
        }
      } else {
        vscode.window.showWarningMessage('当前文件不支持多行注释!如有需要请微信联系 : restyhap')
      }
    })
  });

  context.subscriptions.push(disposable);
}

/**
 * 去除多行注释的前缀和后缀
 * @param {number} startLine 注释开始一行
 * @param {number} endLine 注释结束一行
 * @param {vscode.TextDocument} document 当前访问的文档
 * @param {vscode.TextEditorEdit} editorBuilder 当前编辑器的构建对象
 */
function clear (startLine, endLine, document, editorBuilder) {

  let startLength = document.lineAt(startLine).text.length;
  let startText = document.lineAt(startLine).text.replace(/\/\*/, "");

  let endLength = document.lineAt(endLine).text.length;
  let endText = document.lineAt(endLine).text.replace(/\*\/$/, "");

  editorBuilder.replace(new vscode.Range(new vscode.Position(startLine, 0), new vscode.Position(startLine, startLength)), "");
  editorBuilder.insert(new vscode.Position(startLine, 0), startText);
  editorBuilder.replace(new vscode.Range(new vscode.Position(endLine, 0), new vscode.Position(endLine, endLength)), "");
  editorBuilder.insert(new vscode.Position(endLine, 0), endText);

}
/**
 * 添加多行注释
 * @param {number} currentLine 注释开始的第一行
 * @param {number} anchorLine 注释结束的最后一行
 * @param {vscode.TextDocument} document 当前访问的文档
 * @param {vscode.TextEditorEdit} editorBuilder 当前编辑器的构建对象
 */
function insert (currentLine, anchorLine, data, document, editorBuilder) {

  console.log(" insert ======== ");
  let startLength = document.lineAt(currentLine).text.length;
  let startText = data.prefix + document.lineAt(currentLine).text;

  let endLength = document.lineAt(anchorLine).text.length;
  let endText = document.lineAt(anchorLine).text + data.suffix;

  editorBuilder.replace(new vscode.Range(new vscode.Position(currentLine, 0), new vscode.Position(currentLine, startLength)), "");
  editorBuilder.insert(new vscode.Position(currentLine, 0), startText);
  editorBuilder.replace(new vscode.Range(new vscode.Position(anchorLine, 0), new vscode.Position(anchorLine, endLength)), "");
  editorBuilder.insert(new vscode.Position(anchorLine, 0), endText);

}

function deactivate () { }

module.exports = {
  activate,
  deactivate
}
